package com.cskaoyan.controller;

import com.cskaoyan.bean.market.*;
import com.cskaoyan.bo.market.*;
import com.cskaoyan.service.market.*;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.market.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-08 23:31
 **/

// 商场管理模块
@RestController
public class MarketController {

    // 行政区域
    // /admin/region/list
    @RestController
    @RequestMapping("admin/region")
    public class RegionController {
        @Autowired
        RegionService regionService;

        @GetMapping("list")
        // @RequiresPermissions("admin:region:list")
        public BaseRespVo regionList() {
            RegionDataVo regionDataVo = regionService.regionList();
            return BaseRespVo.ok(regionDataVo.getData());
        }
    }

    // 品牌制造商
    @RestController
    @RequestMapping("admin/brand")
    public class BrandController {
        @Autowired
        BrandService brandService;

        // admin/brand/list?page=1&limit=20&sort=add_time&order=desc
        // ,params = {"page","limit","sort","order"}
        @GetMapping("list")
        @RequiresPermissions("admin:brand:list")
        public BaseRespVo brandList(Integer page,Integer limit,String sort,String order,Integer id,String name) {
            BrandDataVo brandDataVo = brandService.brandList(page, limit, sort, order,id,name);
            return BaseRespVo.ok(brandDataVo);
        }

        ///admin/brand/create
        @PostMapping("create")
        @RequiresPermissions("admin:brand:create")
        public BaseRespVo brandCreate(@RequestBody BrandCreateBo brandCreateBo) {
            BrandCreateVo vo = brandService.createBrand(brandCreateBo);
            return BaseRespVo.ok(vo);
        }

        // /admin/brand/update
        @PostMapping("update")
        @RequiresPermissions("admin:brand:update")
        public BaseRespVo brandUpdate(@RequestBody Brand brand) {
            Brand brand1 = brandService.brandUpdate(brand);
            return BaseRespVo.ok(brand1);
        }

        // /admin/brand/delete
        @PostMapping("delete")
        @RequiresPermissions("admin:brand:delete")
        public BaseRespVo brandDelete(@RequestBody Brand brand) {
            brandService.brandDelete(brand);
            return BaseRespVo.ok();
        }

        // TODO:admin:order:read
    }

    // 商品类目
    @RestController
    @RequestMapping("admin/category")
    public class CategoryController {
        @Autowired
        CategoryService categoryService;

        // /admin/category/list
        @GetMapping("list")
        @RequiresPermissions("admin:category:list")
        public BaseRespVo categoryList() {
            List<CategoryVo> categoryVos = categoryService.categoryList();
            return BaseRespVo.ok(categoryVos);
        }

        // /admin/category/create
        @PostMapping("create")
        @RequiresPermissions("admin:category:create")
        public BaseRespVo categoryCreate(@RequestBody CategoryCreateBo categoryCreateBo) {
            CategoryCreateVo vo = categoryService.createCategory(categoryCreateBo);
            return BaseRespVo.ok(vo);
        }

        // /admin/category/update
        // TODO:这里的功能不完整
        @PostMapping("update")
        @RequiresPermissions("admin:category:update")
        public BaseRespVo categoryUpdate(@RequestBody CategoryVo category) {
            int i = categoryService.categoryUpdate(category);
            BaseRespVo baseRespVo = new BaseRespVo();
            if(i==0){
                baseRespVo.setErrno(621);
                baseRespVo.setErrmsg("编辑失败，不允许把自己放在自己目录下！");
            }
            if(i==1){
                baseRespVo.setErrno(0);
                baseRespVo.setErrmsg("成功");
            }
            if(i==401){
                baseRespVo.setErrno(401);
                baseRespVo.setErrmsg("参数不对，必须有父目录！");
            }
            return baseRespVo;
        }

        // /admin/category/delete
        @PostMapping("delete")
        @RequiresPermissions("admin:category:delete")
        public BaseRespVo categoryDelete(@RequestBody Category category) {
            categoryService.deleteCategory(category);
            return BaseRespVo.ok();
        }

        // /admin/category/l1
        @GetMapping("l1")
        // @RequiresPermissions("admin:category:l1")
        public BaseRespVo categoryL1() {
            List<CategoryL1Vo> l1Vos = categoryService.categoryL1();
            return BaseRespVo.ok(l1Vos);
        }

        // TODO：      "admin:category:read",
    }

    // 订单管理
    @RestController
    @RequestMapping("admin/order")
    public class OrderController {
        @Autowired
        OrderService orderService;

        // /admin/order/list?page=1&limit=20&sort=add_time&order=desc
        @GetMapping("list")
        @RequiresPermissions("admin:order:list")
        public BaseRespVo orderList(Integer page,Integer limit,String sort,String order,
                                    Integer userId,String orderSn,Short[] orderStatusArray) {
            OrderListDataVo orderlistDataVo = orderService.orderList(page, limit, sort, order,userId,orderSn,orderStatusArray);
            return BaseRespVo.ok(orderlistDataVo);
        }

        // /admin/order/detail?id=36
        @GetMapping("detail")
        @RequiresPermissions("admin:order:read")
        public BaseRespVo orderDetail(Integer id) {
            OrderDetailDataVo orderDetailDataVo = orderService.orderDetail(id);
            return BaseRespVo.ok(orderDetailDataVo);
        }

        // /admin/order/refund
        @PostMapping("refund")
        @RequiresPermissions("admin:order:refund")
        public BaseRespVo orderRefund(@RequestBody OrderRefundBo refundBo) {
            Order order = orderService.orderRefund(refundBo);
//            BaseRespVo baseRespVo = new BaseRespVo();
//            if(i==1){
//                baseRespVo.ok();
//            }
//            if(i==0){
//                baseRespVo.setErrno(621);
//                baseRespVo.setErrmsg("订单退款失败");
//            }
//            return baseRespVo;
            return BaseRespVo.ok(order);
        }
        // /admin/order/ship
        @PostMapping("ship")
        @RequiresPermissions("admin:order:ship")
        public BaseRespVo orderShip(@RequestBody OrderShipBo orderShipBo){
            Order order = orderService.orderShip(orderShipBo);
            return BaseRespVo.ok(order);
        }
    }

    // 通用问题
    @RestController
    @RequestMapping("admin/issue")
    public class IssueController {
        @Autowired
        IssueService issueService;

        // /admin/issue/list?page=1&limit=20&sort=add_time&order=desc&question=1
        @GetMapping("list")
        @RequiresPermissions("admin:issue:list")
        public BaseRespVo issueList(Integer page,Integer limit,String sort,String order,String question) {
            IssueListVo issueListVo = issueService.issueList(page, limit, sort, order, question);
            return BaseRespVo.ok(issueListVo);
        }

        // /admin/issue/create
        @PostMapping("create")
        @RequiresPermissions("admin:issue:create")
        public BaseRespVo issueCreate(@RequestBody IssueCreateBo issueCreateBo) {
            Issue issue = issueService.issueCreate(issueCreateBo);
            return BaseRespVo.ok(issue);
        }

        // /admin/issue/update
        @PostMapping("update")
        @RequiresPermissions("admin:issue:update")
        public BaseRespVo issueUpdate(@RequestBody Issue issue) {
            Issue issueVo = issueService.issueUpdate(issue);
            return BaseRespVo.ok(issueVo);
        }

        // /admin/issue/delete
        @PostMapping("delete")
        @RequiresPermissions("admin:issue:delete")
        public BaseRespVo issueDelete(@RequestBody Issue issue) {
            issueService.issueDelete(issue);
            return BaseRespVo.error(0,"成功");
        }
    }

    // 关键词
    @RestController
    @RequestMapping("admin/keyword")
    public class KeywordController {
        @Autowired
        KeywordService keywordService;

        // /admin/keyword/list?page=1&limit=20&sort=add_time&order=desc
        @GetMapping("list")
        @RequiresPermissions("admin:keyword:list")
        public BaseRespVo keywordList(Integer page,Integer limit,String sort,String order,String keyword,String url) {
            KeywordListDataVo keywordListDataVo = keywordService.keywordList(page, limit, sort, order, keyword, url);
            return BaseRespVo.ok(keywordListDataVo);
        }

        // /admin/keyword/create
        @PostMapping("create")
        @RequiresPermissions("admin:keyword:create")
        public BaseRespVo keywordCreate(@RequestBody KeywordCreateBo keywordCreateBo) {
            KeywordCreateVo vo = keywordService.createKeyword(keywordCreateBo);
            return BaseRespVo.ok(vo);
        }

        // /admin/keyword/update
        @PostMapping("update")
        @RequiresPermissions("admin:keyword:update")
        public BaseRespVo keywordUpdate(@RequestBody KeywordCreateVo keywordCreateVo) {
            KeywordCreateVo vo = keywordService.updateKeyword(keywordCreateVo);
            return BaseRespVo.ok(vo);
        }

        // /admin/keyword/delete
        @PostMapping("delete")
        @RequiresPermissions("admin:keyword:delete")
        public BaseRespVo keywordDelete(@RequestBody Keyword keyword) {
            keywordService.deleteKeyword(keyword);
            return BaseRespVo.error(0,"成功");
        }

        // TODO：admin:keyword:read
    }
}
